﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Xna.Framework;
using System.IO;
using System.Xml.Serialization;

namespace GameName2
{
    class GestureDB
    {
        public List<string> Classes { get; private set; }
        public int NumberofIO { get; private set; }
        public List<GestureSequence> SampleSeq { get; private set; }
        private static string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        public enum TYPEFILEREADER { TypeClasses=1,TypeSourcePath=2,TypeOutput=3,TypeRecognizedAs=4}
       
        public GestureDB()
        {
            Classes = new List<string>();
            SampleSeq = new List<GestureSequence>();
        }
       

        public GestureSequence Add(List<Vector2> gestureSeq,string label,int labelIndex)
        {
            //check if gesture empty
            if (gestureSeq == null || gestureSeq.Count == 0)
                return null;
            
            //fill binding list with label
            if (!Classes.Contains(label))
                Classes.Add(label);

            int classIndex = labelIndex;//Classes.IndexOf(label);
            int RecognizedAs = classIndex;

            GestureSequence curSequence = new GestureSequence() {
                ClassesGes = Classes,
                SourcePath = gestureSeq,
                Output = classIndex,
                RecognizedAs = RecognizedAs
            };

            SampleSeq.Add(curSequence);
            SavetoClassLog(curSequence.ClassesGes);
            SavetoSourcePathLog(curSequence.SourcePath);
            SavetoOutput(curSequence.Output);
            SavetoRecognizedAs(curSequence.RecognizedAs);

            return curSequence;
        }

        public static void SavetoClassLog(List<string> classLabel)
        {
            using (StreamWriter w = File.AppendText(folder + @"\logClasses.txt"))
            {
                LogClasses(w, classLabel);
            }
        }

        public static void LogClasses(TextWriter w, List<string> classLabel)
        {
            for (int i = 0; i < classLabel.Count; i++)
                w.WriteLine(classLabel[i]);
        }

        public static void SavetoSourcePathLog(List<Vector2> sequence)
        {
            using (StreamWriter w = File.AppendText(folder + @"\logSourcePath.txt"))
            {
                LogSourcePath(w, sequence);
            }
        }

        public static void LogSourcePath(TextWriter w, List<Vector2> sequence)
        {
            for (int i = 0; i < sequence.Count; i++)
                w.WriteLine("{0}|{1}", sequence[i].X, sequence[i].Y);
            w.WriteLine("-");
        }

        public static void SavetoOutput(int output)
        {
            using (StreamWriter w = File.AppendText(folder + @"\logOutput.txt"))
            {
                LogOutput(w, output);
            }
        }

        public static void LogOutput(TextWriter w, int output)
        {
            w.WriteLine(output);
        }

        public static void SavetoRecognizedAs(int recognized)
        {
            using (StreamWriter w = File.AppendText(folder + @"\logRecognizedAs.txt"))
            {
                LogRecognizedAs(w, recognized);
            }
        }

        public static void LogRecognizedAs(TextWriter w, int recognized)
        {
            w.WriteLine(recognized);
        }

        public List<string> ReadFromFilesLog(int typefile)
        {
            StreamReader r;
            switch (typefile)
            {
                case (int)TYPEFILEREADER.TypeClasses:
                    r = new StreamReader(folder + @"\logClasses.txt");
                    break;
                case (int)TYPEFILEREADER.TypeOutput:
                    r = new StreamReader(folder + @"\logOutput.txt");
                    break;
                case (int)TYPEFILEREADER.TypeRecognizedAs:
                    r = new StreamReader(folder + @"\logRecognizedAs.txt");
                    break;
                default:
                    r = new StreamReader(folder + @"\logClasses.txt");
                    break;
            }

            List<string> Group = new List<string>();
            string[] temp;
            while (!r.EndOfStream)
            {
                string reader = r.ReadLine();
                temp = reader.Split('\n');
                for (int i = 0; i < temp.Length; i++)
                    Group.Add(temp[i]);
            }
            r.Close();
            return Group;
        }

        public int[] ReadFromOutputLog(int numberIO)
        {
            StreamReader r = new StreamReader(folder + @"\logOutput.txt");

            int[] Group = new int[numberIO];
            string[] temp;
            int i = 0;
            while (!r.EndOfStream)
            {
                string reader = r.ReadLine();
                temp = reader.Split('\n');
                Group[i]=((int)Convert.ToInt32(temp[0]));
                i++;
            }
            r.Close();
            return Group;
        }


        public double[][][] ReadFromSourcePathLog()
        {
            StreamReader r = new StreamReader(folder + @"\logSourcePath.txt");
            StreamReader rNumber = new StreamReader(folder + @"\logSourcePath.txt");

            List<Vector2> Direction = new List<Vector2>();
            string[] temp;
            string[] temp2;
            string[] tempNumber;
            double[][][] sequencePerClass;
            int directionCount = 0;
            int count = 0;
            int countNumber = 0;

            // cek contains('-')
            while (!rNumber.EndOfStream)
            {
                string reader = rNumber.ReadLine();
                tempNumber = reader.Split('\n');
                for (int i = 0; i < tempNumber.Length; i++)
                {
                    if (reader.Contains('-'))
                    {
                        countNumber++;
                    }
                }
            }
            rNumber.Close();
            sequencePerClass = new double[countNumber][][];
            NumberofIO = countNumber;

            while (!r.EndOfStream)
            {
                string reader = r.ReadLine();
                temp = reader.Split('\n');
                for (int i = 0; i < temp.Length; i++)
                {
                    if (reader.Contains('-'))
                    {
                        sequencePerClass[count] = GestureSequence.Preprocessing(Direction);
                        Direction.Clear();
                        directionCount = 0;
                        count++;
                    }
                    else
                    {
                        temp2 = temp[i].Split('|');                        
                        for (int j = 0; j < temp2.Length; j += 2)
                        {
                            float x = (float)Convert.ToDouble(temp2[j]);
                            float y = (float)Convert.ToDouble(temp2[j + 1]);
                            Direction.Add(new Vector2(x, y));
                            directionCount++;
                        }
                    }
                }
            }
            r.Close();
    
            return sequencePerClass;
        }


        //public static void Log(TextWriter w,GestureSequence seq)
        //{
        //    w.Write("\r\nLog Entry : ");
        //    w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
        //        DateTime.Now.ToLongDateString());
        //    w.WriteLine("  :\n");
        //    w.WriteLine("Label: {0}\n", seq.ClassesGes[0]);
        //    w.WriteLine("index: {0}\n", seq.Output);
        //    w.WriteLine("-------------------------------");
        //    for (int i = 0; i < seq.SourcePath.Count; i++)
        //        w.WriteLine("Classes X:{0} Y:{1}\n", seq.SourcePath[i].X, seq.SourcePath[i].Y);

        //}
    }
}
